/*  This file is part of Phantom Power.
 *  Copyright (C) 2010 Jacob Dawid
 *
 *  Phantom Power is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Phantom Power is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Phantom Power. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef DEVICE_H
#define DEVICE_H

#include <QObject>
#include <QStringList>
#include <QRegExp>
#include <QSettings>

#ifdef __windows__
	#define _WIN32_WINNT 0x0500
	#include <windows.h>
	#include <winuser.h>
#endif

#ifdef __unix__
	#include <stdio.h>
	#include <stdlib.h>
	#include <string.h>
	#include <unistd.h>
	#include <fcntl.h>
	#include <errno.h>
	#include <linux/input.h>
	#include <linux/uinput.h>
#endif

class device : public QObject
{
	Q_OBJECT
	public:
		device();
		~device();

		enum type
		{
			mouse,
			slider,
			joystick1,
			joystick2,
			joystick2b
		};

	private:
		device::type thisType;
		QSettings* settings;	
		// Linux specific code
		#ifdef __unix__
			int deviceHandle;
			void linuxDeviceInitialize();
			void linuxDeviceDestroy();
			void linuxDeviceReset();

			void linuxCreateMouseDevice();
			void linuxCreateJoystickDevice();
			void linuxCreateKeyboardDevice();

			void linuxHandleMouseIssue(QList<double> issue);
			void linuxHandleSliderIssue(QList<double> issue);
			void linuxHandleJoystick1Issue(QList<double> issue);
			void linuxHandleJoystick2Issue(QList<double> issue);
			void linuxHandleJoystick2bIssue(QList<double> issue);

			void linuxIO(int request, int argument);
			void linuxIO(int request);
			void linuxSendEvent(int type, int code, int value);
			void linuxSynchronize();
		#endif

		#ifdef __windows__
			void windowsDeviceInitialize();
			void windowsDeviceDestroy();
			void windowsDeviceReset();

			void windowsHandleMouseIssue(QList<double> issue);
			void windowsHandleSliderIssue(QList<double> issue);
			void windowsHandleJoystick1Issue(QList<double> issue);
			void windowsHandleJoystick2Issue(QList<double> issue);
			void windowsHandleJoystick2bIssue(QList<double> issue);
		#endif

			QList<double> applyMouseSettings(QList<double> issue);
			QList<double> applyKeyboardSettings(QList<double> issue);
			QList<double> applySliderSettings(QList<double> issue);
			QList<double> applyJoystickSettings(QList<double> issue);			

	public slots:
		void handleIssue(QList<double> issue);
		void handleControlSignal(QString value);
		void synchronize();

	signals:
		void report(QString message);
};

#endif
